package com.jingbian.cppccbackend.controller.admin;

import com.jingbian.cppccbackend.entity.News;
import com.jingbian.cppccbackend.entity.Result;
import com.jingbian.cppccbackend.service.admin.NewsService;
import com.jingbian.cppccbackend.utils.ResultUtil;
import com.jingbian.cppccbackend.entity.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 新闻表，用于存储新闻信息 前端控制器
 * </p>
 *
 * @author yezi
 * @since 2025-03-03
 */
@RestController
@RequestMapping("/admins/news")
public class adminNewsController {

    @Autowired
    private NewsService newsService;

    //获取新闻数据
    @GetMapping("/getNews")
    public Result<PageVO<News>> getNews(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false,defaultValue = "asc") String sortType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String searchText
    ) {
        PageVO<News> news = newsService.getNews(
                pageNum, pageSize, sortType, startDate,endDate, searchText);
        return ResultUtil.success(news);
    }


    //   新增新闻
    @PostMapping("/addNews")
    public Result addNews(@RequestBody News news) {
        return ResultUtil.success(newsService.save(news));
    }

    //   删除新闻
    @DeleteMapping("/deleteNews/{nid}")
    public Result deleteNews(@PathVariable Integer nid) {
        newsService.delete(nid);
        return ResultUtil.success("新闻删除成功");
    }

    // 获取单个新闻数据
    @GetMapping("/getNewsDetail/{nid}")
    public Result<News> getNewsById(@PathVariable Integer nid) {
        News news = newsService.getById(nid);
        return ResultUtil.success(news);
    }

    // 更新新闻
    @PutMapping("/updateNews/{nid}")
    public Result updateNews(@PathVariable Integer nid,@RequestBody News news) {
        newsService.updateNews(nid,news);
        return ResultUtil.success("新闻更新成功");
    }

}
